package com.mb.mp.service.impl;

import com.mb.mp.bo.MpCashDtlBO;
import com.mb.mp.dao.MpCashDtlDOMapper;
import com.mb.mp.dao.MpMemberInfoDOMapper;
import com.mb.mp.domain.MpCashDtlDO;
import com.mb.mp.domain.MpMemberInfoDO;
import com.mb.mp.service.ICashService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 提现管理服务
 *
 * @author
 * @create 2018-09-21 上午9:40
 **/
@Service("cashService")
public class CashServiceImpl implements ICashService {
    private static Logger logger = LoggerFactory.getLogger(CashServiceImpl.class);

    @Autowired
    private MpCashDtlDOMapper cashDtlDOMapper;

    @Autowired
    private MpMemberInfoDOMapper memberInfoDOMapper;

    @Override
    public List<MpCashDtlBO> queryList(Map<String, Object> map) {
        return cashDtlDOMapper.list(map);
    }

    @Override
    public int count(Map<String, Object> map) {
        return cashDtlDOMapper.count(map);
    }

    @Override
    public MpCashDtlDO getCashDtl(Long id) {
        return cashDtlDOMapper.selectByPrimaryKey(id);
    }

    @Override
    @Transactional
    public int audit(MpCashDtlDO cashDtlDO) {
        int cashStatus = cashDtlDO.getCashStatus();
        MpCashDtlDO cashDtl = cashDtlDOMapper.selectByPrimaryKey(cashDtlDO.getId());
        if(cashDtl != null){
            BeanUtils.copyProperties(cashDtlDO,cashDtl);
        }
        if(cashStatus == 2){
            //审核不通过，退回提现金额
            MpMemberInfoDO memberInfo = new MpMemberInfoDO();
            memberInfo.setMemberId(cashDtl.getMemberId());
            memberInfo.setRemainCommissionAmt(cashDtl.getCashAmt());
            memberInfoDOMapper.backCommission(memberInfo);
            logger.info("退回提现金额, id :"+cashDtl.getId());
        }
        cashDtl.setAuditTime(new Date());
        return cashDtlDOMapper.updateByPrimaryKeySelective(cashDtl);
    }
}
